package com.wlcorp.forcazamis.web.action.message;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.message.IMessageService;
import com.wlcorp.forcazamis.domain.message.Message;
import com.wlcorp.forcazamis.domain.message.MessageStaff;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.DateUtil;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.WebUtil;
 
public class MessageAction extends DispatchAction {
	public ActionForward getIndoxList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff=SessionValue.getInstance(request).getStaff();
		//收件箱
		MessageStaff messageStaff=new MessageStaff();
		messageStaff.setType(0);
		messageStaff.setStaff(staff);
		messageStaff.setReceiverId(staff.getStaffId());
		
		List<MessageStaff> indoxList=ContextUtil.getInstance().getMessageService().getMessageStaffList(messageStaff);
		request.setAttribute("indoxList", indoxList);
		return mapping.findForward("indoxList");
	}
	
	public ActionForward getDraftList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff=SessionValue.getInstance(request).getStaff();
		//草稿箱
		MessageStaff messageStaff=new MessageStaff();
		messageStaff.setType(1);
		messageStaff.setStaff(staff);
		
		List<MessageStaff> draftList=ContextUtil.getInstance().getMessageService().getMessageStaffList(messageStaff);
		request.setAttribute("draftList", draftList);
		return mapping.findForward("draftList");
	}
	
	public ActionForward getDeliverList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff=SessionValue.getInstance(request).getStaff();
		//已发箱
		MessageStaff messageStaff=new MessageStaff();
		messageStaff.setType(2);
		messageStaff.setStaff(staff);
		
		List<MessageStaff> deliverList=ContextUtil.getInstance().getMessageService().getMessageStaffList(messageStaff);
		request.setAttribute("deliverList", deliverList);
		return mapping.findForward("deliverList");
	}
	
	public ActionForward getArchiveList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff=SessionValue.getInstance(request).getStaff();
		//存档箱
		MessageStaff messageStaff=new MessageStaff();
		messageStaff.setType(3);
		messageStaff.setStaff(staff);
		
		List<MessageStaff> archiveList=ContextUtil.getInstance().getMessageService().getMessageStaffList(messageStaff);
		request.setAttribute("archiveList", archiveList);
		return mapping.findForward("archiveList");
	}
	
	public ActionForward deleteMessageStaff(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String type=(String)request.getParameter("type");
		MessageStaff messageStaff=new MessageStaff();
		messageStaff.setId(Long.parseLong(request.getParameter("messageStaffId")));
		ContextUtil.getInstance().getMessageService().deleteMessageStaff(messageStaff);
		if (type.equals("indox")) {
			return getIndoxList(mapping,form,request,response);
		}
		if (type.equals("draft")) {
			return getDraftList(mapping,form,request,response);
		}
		if (type.equals("deliver")) {
			return getDeliverList(mapping,form,request,response);
		}
		if (type.equals("archive")) {
			return getArchiveList(mapping,form,request,response);
		}
		return getIndoxList(mapping,form,request,response);
	}
	
	public ActionForward readMessage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		IMessageService messageService=ContextUtil.getInstance().getMessageService();
		MessageStaff messageStaff=messageService.getMessageStaffById(Long.parseLong(request.getParameter("messageStaffId")));
		String type=(String)request.getParameter("type");
		if(type.equals("indox")){
			if(messageStaff.getReadStatus()==0){//更改为已读
				messageStaff.setReadStatus(1);
				messageService.saveMessageStaff(messageStaff);
			}
		}
		request.setAttribute("type", type);
		request.setAttribute("messageStaff", messageStaff);
		return mapping.findForward("readMessage");
	}
	
	public ActionForward sendMessage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String operate=(String)request.getParameter("operate");
		IMessageService messageService=ContextUtil.getInstance().getMessageService();
		
		WebUtil.setResponseNoCache(response);
		Message message = new Message();
		WebUtil.setRequestToObject(request,message);
	 
		message.setSendTime(DateUtil.toDateTimeString(new Date()));
		message.setWriteTime(DateUtil.toDateTimeString(new Date()));
		message.setSender(SessionValue.getInstance(request).getStaff());
		messageService.saveMessage(message);
		Long messageId=messageService.getMaxMessageId();
		
		Staff currentStaff=SessionValue.getInstance(request).getStaff();
		String[] receiverIdStr=message.getReceiverId().split("#");
		for (int i = 0; i < receiverIdStr.length; i++) {
			Long receiverId = Long.parseLong(receiverIdStr[i]);
			MessageStaff messageStaff=new MessageStaff();
			messageStaff.setReceiverId(receiverId);
			
			Message msg=new Message();
			msg.setMessageId(messageId);
			messageStaff.setMessage(msg);
			Staff receiver=new Staff();
			receiver.setStaffId(receiverId);
			messageStaff.setReadStatus(0);
			messageStaff.setSaveStatus(0);
			if (operate.equals("doSend")) {
				messageStaff.setType(0);
				//保存已发送
				messageStaff.setStaff(currentStaff);
				messageStaff.setType(2);
				messageService.saveMessageStaff(messageStaff);

				//发送
				messageStaff.setStaff(receiver);
				messageStaff.setType(0);
				messageService.saveMessageStaff(messageStaff);
				if(AppConstant.BASE_BEAN_STATUS_ERROR == message.getStatus()){
					WebUtil.setAlertMessage(request,message.getMsg());
				}else{
					WebUtil.setAlertMessage(request,"消息发发送成功！");
				}
			}
			if (operate.equals("doDraft")) {
				//保存草稿
				messageStaff.setStaff(currentStaff);
				messageStaff.setType(1);
				messageService.saveMessageStaff(messageStaff);
				
				if(AppConstant.BASE_BEAN_STATUS_ERROR == message.getStatus()){
					WebUtil.setAlertMessage(request,message.getMsg());
				}else{
					WebUtil.setAlertMessage(request,"草稿保存成功！");
				}
			}
		}
		return mapping.findForward("writeMessage");
	}
	
	public ActionForward selectStaffTop(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		 
		return mapping.findForward("readMessage");
	}
	public ActionForward saveMessageStaff(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		IMessageService service=ContextUtil.getInstance().getMessageService();
		Long messageStaffId=Long.parseLong(request.getParameter("messageStaffId").toString());
		MessageStaff messageStaff=service.getMessageStaffById(messageStaffId);
		messageStaff.setType(3);
		messageStaff.setSaveStatus(1);
		service.saveMessageStaff(messageStaff);
		return getArchiveList(mapping, form, request, response);
	}
}
